package com.hujesse.service.user;
import com.hujesse.dao.user.UserDao;
import com.hujesse.pojo.User;
import com.hujesse.utils.QueryInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public Map<String,Object> queryUserByInfo(String username, String password) {
       Boolean flag = false;
       User user = null;
        Map<String, Object> userInfo = null;
        if (!StringUtils.isNullOrEmpty(username)&& !StringUtils.isNullOrEmpty(password)){
           user = userDao.queryUserByInfo(username, password);
           if (null !=user)
               flag = true;
       }
        userInfo = new HashMap<>();
       userInfo.put("flag",flag);
       userInfo.put("user",user);
       return userInfo;
    }

    @Override
    public List<User> queryAllUser(QueryInfo queryInfo) {
        int pageStart = (queryInfo.getPageNum()-1) * queryInfo.getPageSize();
        return  userDao.queryAllUser("%"+queryInfo.getUsername()+"%",pageStart,queryInfo.getPageSize());
    }

    @Override
    public int deleteUser(int id) {
        return userDao.deleteUser(id);
    }

    @Override
    public int addUser(User user) {
        user.setRole("普通用户");
        user.setState(false);
        return userDao.addUser(user);
    }

    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int updateUserRole(User user) {
        return userDao.updateUserRole(user);
    }

    @Override
    public int updateUserState(int id, boolean state) {
        return userDao.updateUserState(id,state);
    }

    @Override
    public int getUserCounts(QueryInfo queryInfo) {
        return userDao.getUserCounts("%"+queryInfo.getUsername()+"%");
    }
}
